package com.formula.datastructure.exercise.stack;

import com.formula.datastructure.util.DataUtil;

public class LinkStack {
    // 推荐用哨兵
    LinkStackNode head = new LinkStackNode(null);

    public boolean isEmpty() {
        return head.next == null;
    }

    public void push(Integer data) {
        LinkStackNode node = new LinkStackNode(data);
        node.next = head.next;
        head.next = node;
    }

    public Integer pop() {
        if (head.next != null) {
            LinkStackNode node = head.next;
            head.next = head.next.next;
            return node.data;
        }
        return null;
    }

    public static void main(String[] args) {
        LinkStack stack = new LinkStack();
        Integer[] input = DataUtil.genUniqueArray(10, 50);
        DataUtil.printArray(input);

        for (Integer data : input) {
            stack.push(data);
        }

        for (int i = 0; i < input.length / 2; i++) {
            System.out.println(stack.pop());
        }
    }
}
